﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using MyServer.Common;
using MyServer.Udp;
using System.Net.Sockets;
using ScreenMonitor.Common;
using System.Net;
using ScreenMonitor.Common.Objects;
using FrameWork.Setting;

namespace ScreenMonitor
{
    /// <summary>
    /// Udp协议相关命令执行中心
    /// </summary>
    [SettingParentDefine]
    public class UdpCommandCenter : DataOperatorBase
    {
        public event OperateCommandEventHandler OperateCommand;

        #region========私有变量========

        private object LockObj = new object();
        private object SendLockObj = new object();
        public UdpClient LocalUdpClient { get; set; }

        #endregion

        protected override bool Operate(ref DataPackageCollection pkg, out DataPackageCollection RePkg)
        {
            RePkg = null;
            if (OperateCommand != null)
            {
                OperateCommand(this, new OperateCommandEventArgs(pkg));
            }
            return true;
        }

        public void SendCommand(string command, IPEndPoint RemotePoint)
        {
            lock (SendLockObj)
            {
                CommandObjects.UdpCenter.SendTo(LocalUdpClient, RemotePoint, new Byte[] { 0 }, Util.GetpkgName(command), true);
            }
        }

        public void SendCommandWithData(string command, byte[] data, IPEndPoint RemotePoint)
        {
            lock (SendLockObj)
            {
                CommandObjects.UdpCenter.SendTo(LocalUdpClient, RemotePoint, data, Util.GetpkgName(command), true);
            }
        }

        public UdpCommandCenter(UdpCenter center) : base(center) { }

    }
}
